﻿using UnityEngine;
using System.Collections;

public class Deck : MonoBehaviour {

	public int totalNumCards;
	public int currentNumCards;
	//private int maxDeckSize = 15;
	public Card[] deck = new Card[15];

	public bool playerOneDeck;
	public Card card;
	public GameObject cardBackPrefab;

	// Use this for initialization
	void Start () {
		//deck = new Card[15];
		//deck[0] = card;
	}

	public void shuffle(){
		int currentSize = 0;
		for(int i = 0; i < deck.Length; i++){
			if(deck[i]!=null)
				currentSize++;
			else
				break;
		}
		Card[] tempDeck = new Card[15];
		int index = 0;
		while (index < currentSize){
			int randomNumber = (int)Random.Range(0,currentSize);
			if(tempDeck[randomNumber]==null){
				tempDeck[randomNumber] = deck[index];
				index++;
			}
		}
		for(int j = 0; j < currentSize; j++){
			deck[j] = tempDeck[j];
		}
	}
	//function for drawing a card
	public void draw(){
		//no more cards to draw
		if(deck[0] == null){
			Debug.Log ("Deck Out Of Cards");
			return;
		}
		deck[0].inHand = true;
		//draw a card from the appropriate deck and add it to the appropriate hand
		if(playerOneDeck){
			if(GameController.gc.playerOneHand.currentSize==7){
				Debug.Log ("Hand Too Full");
				return;
			}
			int currentSize = GameController.gc.playerOneHand.currentSize;
			GameController.gc.playerOneHand.hand[currentSize] = deck[0];
			GameController.gc.playerOneHand.hand[currentSize].handPositon = currentSize;
			GameController.gc.playerOneHand.hand[currentSize].transform.position = GameController.gc.playerOneHand.transform.position + new Vector3(currentSize+1,0,0);
			GameController.gc.playerOneHand.hand[currentSize].transform.parent = GameController.gc.playerOneHand.transform;
			GameController.gc.playerOneHand.currentSize++;
		}
		else{
			if(GameController.gc.playerTwoHand.currentSize==7){
				Debug.Log ("Hand Too Full");
				return;
			}
			int currentSize = GameController.gc.playerTwoHand.currentSize;
			GameController.gc.playerTwoHand.hand[currentSize] = deck[0];
			GameController.gc.playerTwoHand.hand[currentSize].handPositon = currentSize;
			GameController.gc.playerTwoHand.hand[currentSize].transform.position = GameController.gc.playerTwoHand.transform.position + new Vector3(currentSize,0,0);
			GameController.gc.playerTwoHand.hand[currentSize].transform.parent = GameController.gc.playerTwoHand.transform;
			GameObject myObject = (GameObject)Instantiate(cardBackPrefab, GameController.gc.playerTwoHand.hand[currentSize].transform.position, Quaternion.identity);
			myObject.transform.parent = GameController.gc.playerTwoHand.hand[currentSize].transform;
			GameController.gc.playerTwoHand.currentSize++;
		}
		//call the afterdraw function
		afterDraw();

	}
	//bring the cards in the deck array forward one after drawing;
	private void afterDraw(){
		int currentIndex = 0;
		while(currentIndex < 14 && deck[currentIndex+1]!=null){
			deck[currentIndex] = deck[currentIndex+1];
			currentIndex++;
		}
		deck[currentIndex] = null;
	}

	public void saveChanges(){

	}

	public void addCardsToDeck(){

	}
}
